<template>
	<view class="address-list">
		<view class="address-list-container">
			<view class="list">
				<view
					v-for="address in addressList"
					:key="address.id"
					class="item"
					@click="onAddressSelect(address)">
					<view class="address">
						<view class="name">收货人: {{ address.real_name }} <text class="phone">{{ address.phone }}</text></view>
						<view class="detail">收货地址: {{ address.province }}{{ address.city }}{{ address.district }}{{ address.detail }}</view>
					</view>
					<view class="opts">
						<view class="edit-btn" @click.stop="onEditAddress(address)">
							<text class="iconfont icon-bianji"></text>
							编辑
						</view>
						<view class="delete-btn" @click.stop="onDeleteAddress(address)">
							<text class="iconfont icon-shanchu"></text>
							删除
						</view>
					</view>
				</view>
			</view>
			<Empty v-if="noneAddress"></Empty>
			<NoMore v-show="noMore"></NoMore>
			
			<view class="opt-btn">
				<navigator :url="`/pages/address_add/address_add?from=${fromPage}`" hover-class="none">
					<button class="add-address-btn">添加新地址</button>
				</navigator>
			</view>
		</view>
	</view>
</template>

<script>
	import { mapMutations } from 'vuex'
	import {
		addressList as addressListApi,
		addressDelete as addressDeleteApi
	} from '@/api/address'

	import NoMore from '@/components/NoMore'
	import Empty from './components/Empty'
	export default {
		components: {
			Empty,
			NoMore
		},
		data() {
			return {
				fromPage: '',
				params: {
					page: 1,
					limit: 20,
				},
				addressList: [],
				noMore: false
			}
		},
		computed: {
			noneAddress () {
				return this.addressList.length === 0
			}
		},
		onLoad ({ from }) {
			this.fromPage = from || ''
		},
		onShow () {
			this.params.page = 1
			this.getAddressList()
		},
		methods: {
			...mapMutations({
				SET_CURRENT_ADDRESS: 'SET_CURRENT_ADDRESS'
			}),
			onAddressSelect (address) {
				if (this.fromPage === 'order_confirm') {
					this.SET_CURRENT_ADDRESS(address)
					uni.navigateBack()
				}
			},
			onDeleteAddress (address) {
				uni.showModal({
				    title: '提示',
				    content: '确定要删除此收货地址吗？',
				    success: (res) => {
				        if (res.confirm) {
				            this.addressDelete(address)
				        }
				    }
				})
			},
			onEditAddress (address) {
				uni.navigateTo({
					url: `/pages/address_add/address_add?from=${this.fromPage}&id=${address.id}`
				})
			},
			async getAddressList () {
				if (this.noMore) {
					return false
				}
				const { status, data, msg } = await addressListApi(this.params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					if (this.params.page > 1) {
						this.addressList = [...this.addressList, ...data]
						  
						if (data.length === 0) {
							this.noMore = true
						}
					} else {
						this.addressList = data
					}
					  
					if (data.length) {
						this.params.page++
					}
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			},
			async addressDelete (address) {
				const params = {
					id: address.id
				}
				const { status, msg } = await addressDeleteApi(params)
				if (status === this.API_STATUS_CODE.SUCCESS) {
					this.params.page = 1
					this.getAddressList()
					this.SET_CURRENT_ADDRESS(null)
					uni.showToast({
						icon: 'none',
						title: '删除成功',
						duration: 3000
					})
				} else {
					uni.showToast({
						icon: 'none',
						title: msg,
						duration: 3000
					})
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
.address-list {
	&-container {
		.list {
			.item {
				padding: 0 30rpx;
				margin-bottom: 12rpx;
				
				.address {
					padding: 30rpx 0;
					border-bottom: 1rpx solid #eee;
					font-size: 28rpx;
					color: #282828;
					.name {
						font-size: 28rpx;
						font-weight: 700;
						margin-bottom: 8rpx;
					}
					.phone {
						margin-left: 10rpx;
					}
				}
				
				.opts {
					display: flex;
					justify-content: flex-end;
					align-items: center;
					height: 82rpx;
					font-size: 28rpx;
					color: #282828;
					.iconfont {
						font-size: 28rpx;
						margin: 0 10rpx 0 40rpx;
					}
				}
			}
		}
		.opt-btn {
			position: fixed;
			width: 100%;
			background-color: #fff;
			bottom: 0;
			height: 106rpx;
			padding: 0 30rpx;
			box-sizing: border-box;
			height: calc(106rpx+ constant(safe-area-inset-bottom));
			
			.add-address-btn {
				width: 690rpx;
				height: 76rpx;
				border-radius: 50rpx;
				text-align: center;
				line-height: 74rpx;
				font-size: 30rpx;
				color: #fff;
				background-color: #e93323;
			}
		} 
	}
}
</style>

